#ifndef _2CANBootloaderDefs__h_
#define _2CANBootloaderDefs__h_

#include "../Common/Os Abstraction/stdint.h"

#pragma pack(push,2)

#define MAX_DATA_BYTES_IN_APP_PACKET	1024	

#define BL_COMM_CMD_HEADER_SIZE			(sizeof(BL_COMM_Cmd) - MAX_DATA_BYTES_IN_APP_PACKET)
#define BL_COMM_RESP_HEADER_SIZE		(sizeof(BL_COMM_Resp) - MAX_DATA_BYTES_IN_APP_PACKET)

#if (MAX_DATA_BYTES_IN_APP_PACKET & 1)
	#error MAX_DATA_BYTES_IN_APP_PACKET must be even
#endif

#define BL_COMM_2CAN_CHIP_ID	(0x0000)

#define BL_COMM_FRAME_HEADER 0xAACC

#define BL_COMM_CMD_POLL			(0)
#define BL_COMM_CMD_GET_INFO		(1)
#define BL_COMM_CMD_VALIDATE_APP	(2)
#define BL_COMM_CMD_AUTHENTICATE	(3)
#define BL_COMM_CMD_ERASE			(4)
#define BL_COMM_CMD_PUT_DATA		(5)
#define BL_COMM_CMD_WRITE			(6)
#define BL_COMM_CMD_READ			(7)
#define BL_COMM_CMD_GET_DATA		(8)
#define BL_COMM_CMD_WRITE_USER_SECTOR	(9)
#define BL_COMM_CMD_READ_USER_SECTOR	(10)
#define BL_COMM_CMD_GETDATA				(11)
#define BL_COMM_CMD_RUN_APP				(12)
#define BL_COMM_CMD_RESET				(13)
#define BL_COMM_CMD_ENTER_BL			(14)
#define BL_COMM_CMD_GETIPSETTINGS		(15)
#define BL_COMM_CMD_SETIPSETTINGS		(16)
#define BL_COMM_CMD_DEFALTIPSETTINGS	(17)
#define BL_COMM_CMD_TEST_EEPROM			(18)

typedef enum _BL_COMM_ErrorCode_t
{
	BL_COMM_ErrorCode_Ok,
	BL_COMM_ErrorCode_BadLen,
	BL_COMM_ErrorCode_BadHeader,
	BL_COMM_ErrorCode_BadCheckSum,
	BL_COMM_ErrorCode_InvalidCmd,
	BL_COMM_ErrorCode_NonSpecific,
}BL_COMM_ErrorCode_t;
	
	
typedef struct _BL_COMM_Cmd
{
	uint16_t iHeader;
	uint16_t iLen;
	uint16_t iCommand;
	uint16_t iSeed;
	uint16_t iChipID;
	uint16_t iChkSum;
	uint16_t iReserved[3];
	union
	{
		uint8_t iAppPacketBytes[MAX_DATA_BYTES_IN_APP_PACKET];
		uint16_t iAppPacketWords[MAX_DATA_BYTES_IN_APP_PACKET>>1];
	};
}BL_COMM_Cmd;
	
typedef struct _BL_COMM_Resp
{
	uint16_t iHeader;
	uint16_t iLen;
	uint16_t iCommand;
	uint16_t iSeed;
	uint16_t iChipID;
	uint16_t iChkSum;
	uint16_t iRespCode;
	uint16_t iReserved[3];
	union
	{
		uint8_t iAppPacketBytes[MAX_DATA_BYTES_IN_APP_PACKET];
		uint16_t iAppPacketWords[MAX_DATA_BYTES_IN_APP_PACKET>>1];
	};
}BL_COMM_Resp;
	
typedef struct _BL_COMM_RxPacketInfo
{
	#define BL_COMM_RxPacketInfo_Status_InBootloader	(1)
	#define BL_COMM_RxPacketInfo_Status_InApplication	(2)
	uint16_t iStatus;
	
	uint16_t iFirmwareMajor;
	uint16_t iFirmwareMinor;
	
	uint16_t iBootloaderMajor;
	uint16_t iBootloaderMinor;
	
	uint16_t iSerialNo[4];
	
	uint16_t iManDay;
	uint16_t iManMonth;
	uint16_t iManYear;
	
	uint16_t iHardwareRevMajor;
	uint16_t iHardwareRevMinor;
	
	uint16_t iChipID;
	uint16_t iLastError;

	uint16_t uiIP[6];
	
	uint16_t iReserved[4];
}BL_COMM_RxPacketInfo;
	
typedef struct _BL_COMM_CmdData_PutData
{
	uint16_t iOffset;
	uint16_t iLen;
	
	uint8_t iData[1]; // stub to get to rest of app packet
	
}BL_COMM_CmdData_PutData;
	
typedef struct _BL_COMM_CmdData_Write
{
	uint32_t iSectorAddress;
	
}BL_COMM_CmdData_Write,BL_COMM_CmdData_Read;

typedef struct _BL_COMM_CmdData_GetData
{
	uint16_t iOffset;
	uint16_t iLen;
	}BL_COMM_CmdData_GetData;

	typedef struct __MAC_ADDRESS
	{
		BYTE v[6];
	} MAC_ADDRESS;

typedef union _IP_ADDRESS
{
    DWORD Val;
	WORD w[2];
    BYTE v[4];
} IP_ADDRESS;

typedef struct _BL_COMM_IPSettings
{
	IP_ADDRESS		MyIPAddr;
	IP_ADDRESS		MyMask;
	IP_ADDRESS		MyGateway;
	IP_ADDRESS		PrimaryDNSServer;
	IP_ADDRESS		SecondaryDNSServer;
	IP_ADDRESS		DefaultIPAddr;
	IP_ADDRESS		DefaultMask;
	char		NetBIOSName[16];

	uint8_t Flags;

	MAC_ADDRESS	MyMACAddr;

}BL_COMM_IPSettings;

#pragma pack(pop)

#endif // 2CANBootloaderDefs__h_


